﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoD4_Helper
{
    static class OSUtils
    {
        /// <summary>
        /// Local Application Data directory
        /// </summary>
        public static string AppDataDir { 
            get 
            { 
                return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); 
            }
        }

        /// <summary>
        /// Stores the version of the OS
        /// </summary>
        public static OSVersions OsVers { get; private set; }

        /// <summary>
        /// Stores the version of the OS
        /// </summary>
        public static OSArchitecture OsArch { get; private set; }
        
        /// <summary>
        /// Get Operating System information.
        /// </summary>
        static OperatingSystem os = Environment.OSVersion;
        
        /// <summary>
        /// Get version information about the os.
        /// </summary>
        static Version vs = Environment.OSVersion.Version;

        /// <summary>
        /// Gathers information about the runnig OS
        /// </summary>
        /// <returns>Information about the OS such as Version and Architecture</returns>
        public static void DetectOS()
        {
            if (os.Platform == PlatformID.Win32NT)
            {
                switch (vs.Major)
                {
                    case 5:
                        if (vs.Minor == 0)
                            OsVers = OSVersions.Win2000;
                        else
                            OsVers = OSVersions.WinXP;
                        break;
                    case 6:
                        if (vs.Minor == 0)
                            OsVers = OSVersions.WinVista;
                        else
                            OsVers = OSVersions.Win7;
                        break;
                }
            }

            OsArch = Environment.Is64BitOperatingSystem ? OSArchitecture.x64 : OSArchitecture.x86;
        }

        /// <summary>
        /// Get the OS Verion
        /// </summary>
        public static string GetOsVersion()
        {
            //Variable to hold our return value
            StringBuilder sbOS = new StringBuilder();

            if (os.Platform == PlatformID.Win32NT)
            {
                switch (vs.Major)
                {
                    case 5:
                        if (vs.Minor == 0)
                            sbOS.Append("2000");
                        else
                            sbOS.Append("XP");
                        break;
                    case 6:
                        if (vs.Minor == 0)
                            sbOS.Append("Vista");
                        else
                            sbOS.Append("7");
                        break;
                    default: return "unsupported version";
                }
            }

            if (sbOS.Length != 0)
            {
                sbOS.Insert(0, "Windows ");
                sbOS.Append(" ");
                sbOS.Append((Environment.Is64BitOperatingSystem ? 64 : 32) + "-bit");
            }

            return sbOS.ToString();
        }
    }

    enum OSVersions
    {
        Win2000,
        WinXP,
        WinVista,
        Win7
    }

    enum OSArchitecture
    {
        x86,
        x64
    }
}
